package git.soulbgm.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 贺瑞杰
 * @version V1.0
 * @date 2019-04-01 10:09
 * @description
 */
public class ClassUtil {

    private static final char UNDERLINE = '_';
    private static final String GET = "get";
    private static final String IS = "is";
    private static final String SET = "set";

    /**
     * 驼峰转下划线
     * @param param 驼峰字符
     * @return 下划线字符
     */
    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param)) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 下划线转驼峰
     * @param param 驼峰字符
     * @return 下划线字符
     */
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param)) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 通过给字段名称构建出相应的get方法名称
     *
     * @param fieldName 字段名称
     * @return 组合之后的名称
     */
    public static String buildGetMethod(String fieldName, boolean isBoolean) {
        StringBuilder sb = new StringBuilder();
        if (isBoolean) {
            sb.append(IS);
        } else {
            sb.append(GET);
        }
        sb.append(Character.toUpperCase(fieldName.charAt(0)));
        sb.append(fieldName.substring(1));
        return sb.toString();
    }

    /**
     * 通过给字段名称构建出相应的set方法名称
     *
     * @param fieldName 字段名称
     * @return 组合之后的名称
     */
    public static String buildSetMethod(String fieldName) {
        StringBuilder sb = new StringBuilder(SET);
        sb.append(Character.toUpperCase(fieldName.charAt(0)));
        sb.append(fieldName.substring(1));
        return sb.toString();
    }

    /**
     * 通过反射的方式执行getXxx()方法
     *
     * @param obj   实体对象
     * @param field 字段
     * @return 执行了getXxx()方法之后的值
     */
    public static Object executeGetMethod(Object obj, Field field) {
        if (obj == null) {
            return null;
        }
        try {
            return obj.getClass().getMethod(buildGetMethod(field.getName(), field.getType() == Boolean.class)).invoke(obj);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            return null;
        }
    }

    /**
     * 通过反射的方法执行setXxx(xx)方法
     *
     * @param obj   实体对象
     * @param field 字段
     * @param val   执行set方法的值
     * @return 成功返回true，失败false
     */
    public static boolean executeSetMethod(Object obj, Field field, Object val) {
        if (obj == null) {
            return false;
        }
        try {
            if (val != null) {
                if (val.getClass() == field.getType()) {
                    Method method = obj.getClass().getMethod(buildSetMethod(field.getName()), field.getType());
                    method.invoke(obj, val);
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            return false;
        }
    }

}
